%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%      DATA PREPARATION: Table 3  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [yy, ylag, tlag] = prepdat3(gdp, ter, pop, tcap, differ)

%%%----------------------------------------------------------------
%%% gdp - GDP per capita
%%% ter - terrorist attacks
%%% pop - population
%%% tcap = 1 if terrorism to be divided by 1000; 2 = ter/pop
%%% differ = 1 if terrorism to be at level; 2 = first differenced
%%%-----------------------------------------------------------------

y1 = gdp;
if tcap == 1; ter1 = ter/1000;
elseif tcap==2; ter1=ter./pop; end

T = length(y1);
wy = log(y1); 

% First difference
wy = wy(2:T,:) - wy(1:T-1,:);
if differ == 1; wt = ter1(2:T,:); 
elseif differ==2; wt = ter1(2:T,:) - ter1(1:T-1,:); end  

T = length(wy); % We loose 1 year in 1st differencing

% Lagged variables
yy = wy(2:T,:); % loose 1 year because ylag & tlag below
ylag = wy(1:T-1,:); 
tlag = wt(1:T-1,:);  

% Demean
yy = yy - repmat(mean(yy),T-1,1);
ylag = ylag - repmat(mean(ylag),T-1,1);
tlag = tlag - repmat(mean(tlag),T-1,1);

